$(function(){
	var visitor = new Vue({
    el: "#visitor",
    data: {
      currentFunction: null,
      visitorList: [],
      visitorStatus: null,
      visitorCount: null,
      listimgurl : baseURL + "/visitorexcel",
    },
    methods: {
      finish: function(data,status){
        if(status == 3){
          layer.msg('请勿重复操作', {icon: 5});
          return false;
        }else if(status == 2){
          layer.msg('本条记录已结束', {icon: 5});
          return false;
        }else{
          $.ajax({
            type: "GET",
            url: baseURL + '/adminvisitorFinish/'+data,
            success: function(response) { 
              layer.msg(response.message, {icon: 1});
              refreshVisitorManage();
            },
            error: function(response) {
              layer.msg(response.message, {icon: 2});
            }
          });
        }

      },
      delete: function(id,name){
        layer.confirm('确认删除接待老师为'+name+'的用户?', {icon: 3, title:'提示'}, function(index){
          $.ajax({
            type: "POST",
            url: baseURL + '/delVisitor/'+id,
            success: function(data) {
              layer.msg(data.message, {icon: 1});
              refreshVisitorManage();
            },
            error: function(response) {
              layer.msg(response.message, {icon: 2});
            }
          });
        })
      },
      search: function(){
        //搜索条件
        var teacher = $("#searchVisitor").val();
        var data = {};
        data.status = visitor.visitorStatus;
        data.name = teacher;    
        paging(data);
        layuiPage();
      },
      export: function(){
        var teacher = $("#visitorDate").val();
        console.log(teacher)
      }
    }
  });
  $(document).on("_changeCurrentFunction", function(e, currentFunction) {
    switch (visitor.currentFunction) {
      case "访客管理":
        break;
      default:
        if (currentFunction == "访客列表") {
          allCounts();
          refreshVisitorManage();
        }
        break;
    }
    visitor.currentFunction = currentFunction;
  });
  // 初始化加载列表
  var refreshVisitorManage = function() {
    var index = layer.load(0, {
      shade: [0.2,'#fff']
    });
    var data = {};
    paging(data);
    layuiPage();
    layer.close(index);
  };
  // layui分页组件
  var layuiPage = function(){
    layui.use(['laypage', 'layer'], function(){
      var laypage = layui.laypage
      ,layer = layui.layer;
        laypage.render({
        elem: 'vPage'
        ,count: visitor.visitorCount
        ,layout: ['count', 'prev', 'page', 'next', 'limit']
        ,jump: function(obj,first){
            console.log(obj)
            var data = {};
            data.limit = obj.limit;
            data.offset = obj.curr;
            data.status = visitor.visitorStatus;
            data.name = $("#searchVisitor").val();      
            paging(data);
        }
      });
    })
  }
  // 初始化总条数
  function allCounts(){
    $.ajax({
      type: "GET",
      async:false, 
      url: baseURL + '/VisitorCounts',
      success: function(response) { 
        visitor.visitorCount = response;
      },
      error: function(response) {
        layer.msg(response.message, {icon: 2});
      }
    });
  }
  // 分页请求
  function paging(data){
    $.ajax({
      type: "GET",
      async:false,
      url: baseURL + '/pageVisitor',
      data:data,
      success: function(data) {
        visitor.visitorCount = data.length;
        for (var i = data.length - 1; i >= 0; i--) {
          // status 时间
          if( data[i].status == 0 ){
            data[i].replace = '申请访问'; 
          }else if( data[i].status == 1 ){
            data[i].replace = '同意访问'; 
            data[i].leave_time = ''; 
          }else if( data[i].status == 2 ){
            data[i].replace = '拒绝访问'; 
            data[i].accept_time = '';
            data[i].leave_time = '';
          }else if(data[i].status == 3 ){
            data[i].replace = '结束访问'; 
          }
        }
        visitor.visitorList = data;
      },
      error: function(data) {
        layer.alert(response.message,{
            icon : 2,
        })
      },
    });
  }
  // 教师搜索
  $("#searchVisitor").keyup(function () {
    var kw = jQuery.trim($(this).val());
    if (kw == "") {
        // $("#append").hide().html("");
        return false;
    }
    var html = "";
    // 数据集
    $.ajax({
      type: "GET",
      url: baseURL + '/searchVisitor/' + kw,
      success: function(data) { 
        console.log(data)
        for (var i = 0; i < data.length; i++) {
            if (data[i].indexOf(kw) >= 0) {
                html = html + "<div class='layui-input' style='width: 300px;padding: 9px 5px;cursor: pointer' onmouseenter='getFocus(this)' onClick='getCon(this);'>" + data[i] + "</div>"
            }
            if (html != "") {
          $("#append").show().html(html);
      } else {
          $("#append").hide().html("");
      }
        }
      },
      error: function(response) {

      }
    });
  });
 
});
// 点击
function getFocus(obj) {
  $(".layui-input").removeClass("addbg");
  $(obj).addClass("addbg");
}
// 背景颜色渲染
function getCon(obj) {
  var value = $(obj).text();
  $("#searchVisitor").val(value);
  $("#append").hide().html("");
}
